//
//  ViewController.swift
//  swift-03
//
//  Created by xuPeng on 2017/8/4.
//  Copyright © 2017年 陈旭鹏. All rights reserved.
//

import UIKit
import AVKit
import AVFoundation

/// 屏幕高度
let kScreenHeight = UIScreen.main.bounds.size.height;
/// 屏幕宽度
let kScreenWidth = UIScreen.main.bounds.size.width;

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var videoTableView: UITableView!
    var btn:UIButton!
    var videoScreen: UIImageView!
    var playViewController = AVPlayerViewController()
    var playerView = AVPlayer()
    var data = [
        
        video(image: "videoScreenshot00", title: "Introduce 3DS Mario", source: "Youtube - 06:32"),
        video(image: "videoScreenshot01", title: "Emoji Among Us", source: "Vimeo - 3:34"),
        video(image: "videoScreenshot02", title: "Seals Documentary", source: "Vine - 00:06"),
        video(image: "videoScreenshot03", title: "Adventure Time", source: "Youtube - 02:39"),
        video(image: "videoScreenshot04", title: "Facebook HQ", source: "Facebook - 10:20"),
        video(image: "videoScreenshot05", title: "Lijiang Lugu Lake", source: "Allen - 20:30")
        
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
         videoTableView = UITableView.init(frame:CGRect(x:0.0,y:64.0,width:kScreenWidth,height:kScreenHeight-64),style:.plain)
        videoTableView.dataSource = self
        videoTableView.delegate = self
        
        //隐藏滚动条
        videoTableView.showsVerticalScrollIndicator = false
        videoTableView.showsHorizontalScrollIndicator = false
        
//        videoTableView.estimatedSectionFooterHeight = 0.1
//        videoTableView.estimatedSectionHeaderHeight = 0.1
        
        self.view .addSubview(videoTableView)
        
        videoTableView.register(TableViewCell.self, forCellReuseIdentifier: "idstring")
       
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 90
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let reuStr:String = "idstring"
        let cell = videoTableView?.dequeueReusableCell(withIdentifier: reuStr, for: indexPath) as! TableViewCell
        let video = data[indexPath.row]
        
        cell.videoScreenshot.image = UIImage(named: video.image)
        cell.videoTitleLabel.text = video.title
        cell.videoSourceLabel.text = video.source
        
        return cell
        
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    //选中cell时触发这个代理
    open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        
        self.videoScreen = UIImageView()
        self.videoScreen?.frame = CGRect(x:0, y:64, width:kScreenWidth, height:kScreenHeight-200)
        self.videoScreen?.backgroundColor = UIColor.lightGray

        self.videoScreen?.image = UIImage(named: data[indexPath.row].image)
        self.view .addSubview(self.videoScreen)
        
        self.btn = UIButton()
        self.btn.frame = CGRect(x:0, y:64, width:50,height:50)
        self.btn.setTitle("close", for: .normal)
        self.btn .addTarget(self, action: #selector(btntouch), for: .touchUpInside)
        self.view .addSubview(self.btn)
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
    func btntouch() {
        self.videoScreen?.frame = CGRect(x:0, y:64, width:0, height:0)
        self.btn.frame = CGRect(x:0, y:64, width:0, height:0)
        
        let path = Bundle.main.path(forResource: "emoji zone", ofType: "mp4")
        
        playerView = AVPlayer(url: URL(fileURLWithPath: path!))
        
        playViewController.player = playerView
        
        self.present(playViewController, animated: true) {
            self.playViewController.player?.play()
        }

    }
}

